package uk.ac.rothamsted;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.List;
import java.util.Set;

import org.junit.Test;

import uk.ac.rothamsted.utils.OrthologCluster;
import uk.ac.rothamsted.utils.OrthologClusterSet;

public class MainTestPartB {

	@Test
	public void test() throws IOException, ClassNotFoundException {
		InParanoid inParanoid = new InParanoid(50, 50); // this sets the score and overlap cutoff at 50 for each.
		long start = System.currentTimeMillis();



		//load all data files generated in part 1.
		FileInputStream avgSetinStream = new FileInputStream ("src/test/resources/temp/avgSet.data");
		FileInputStream bestHitsABinStream = new FileInputStream ("src/test/resources/temp/BestHitsAB.data");
		FileInputStream speciesAinStream = new FileInputStream ("src/test/resources/temp/speciesA.data");
		FileInputStream speciesBinStream = new FileInputStream ("src/test/resources/temp/speciesB.data");

		ObjectInputStream avgSetin = new ObjectInputStream (avgSetinStream);
		ObjectInputStream bestHitsABin = new ObjectInputStream (bestHitsABinStream);
		ObjectInputStream speciesAin = new ObjectInputStream (speciesAinStream);
		ObjectInputStream speciesBin = new ObjectInputStream (speciesBinStream);

		@SuppressWarnings("unchecked")
		Set<BlastData> avgSet = (Set<BlastData>) avgSetin.readObject();
		@SuppressWarnings("unchecked")
		List<BlastData> bestHitsAB = (List<BlastData>) bestHitsABin.readObject();
		@SuppressWarnings("unchecked")
		Set<String> speciesA = (Set<String>)speciesAin.readObject();
		@SuppressWarnings("unchecked")
		Set<String> speciesB = (Set<String>)speciesBin.readObject();



		System.out.print("Loaded data - ");
		System.out.print((System.currentTimeMillis()-start)/1000F);
		System.out.println(" s");
		start = System.currentTimeMillis();





		//find orthologs
		OrthologClusterSet orthoClusterSet = new OrthologClusterSet(avgSet, speciesA, speciesB);

		System.out.println("Number of potential ortholog Clusters " + bestHitsAB.size());


		//populate clusters with paralogs.
		
		System.out.println("List of all overlap rules used");
		for(int i = bestHitsAB.size()-1; i>=0;i--){
			BlastData biDirHit = bestHitsAB.get(i);
			orthoClusterSet.createFilledCluster(biDirHit);
			inParanoid.merge(orthoClusterSet.getCluster(biDirHit.id1,biDirHit.id2));
		}

		System.out.println("Numberof Ortholog Clusters found - " + orthoClusterSet.size() + "\n");


		System.out.print("Created OrthologClusterSet and Clusters - ");
		System.out.print((System.currentTimeMillis()-start)/1000F);
		System.out.println(" s");
		start = System.currentTimeMillis();



		//list how many orthologs have 0 paralogs, 1 paralogs etc.
		orthoClusterSet.printClusterSize();





		System.out.println("Numberof Ortholog Clusters after Merge - " + orthoClusterSet.size() + "");
		orthoClusterSet.printClusterSize();
		System.out.println("\n\n");

		//print all orthologs and paralogs and which cluster they are found in.
		int x=1;
		
		System.out.println("Ortholog A,\tOrthologB,\tMain Ortholog Score,\tCluster Number,\tParalog Name,\tParalog Score");
		for(OrthologCluster iter : orthoClusterSet.getClusterList()){
			System.out.print(iter.print(x));
			x++;
		}

		//		System.out.print("\n\n\n" + orthoClusterSet + "\n\n\n");
		System.out.print("\n\nFinished mergeing overlap - ");
		System.out.print((System.currentTimeMillis()-start)/1000F);
		System.out.println(" s");
		start = System.currentTimeMillis();


	}
}
